#include "IFUException.h"
#include "FITSImage.h"
#include "GaussParameters.h"
#include "Mask.h"
#include "NonLinearFit.h"
#include "Sifus.h"

extern "C" {
#include <stdlib.h>
}

#include <iostream>
#include <string>

using namespace std;

Mask::Mask(const string &filename, Sifus *sifus) {

  cout << "-> Mask: " << filename << endl;

  try {
    this->fits = new FITSImage(filename);
    this->par = new GaussParameters();
    this->sifus = sifus;
    this->currCol = 0;
    this->currPeak = 0;
    this->filename = filename;
  } catch (IFUException e) {
    e.what();
    exit(1);
  }
}

Mask::~Mask() {
  delete fits;
  delete par;
}

void Mask::process() {

  cout << "-> Fiting..." << endl;

  NonLinearFit *fit = new NonLinearFit(this, sifus);
  
  // loop nas n colunas do arquivo
  int cols = fits->getNcols();

  try {

    for (int i = 0; i < cols; i++) {

      //cout << "coluna " << i << endl;

      this->setCurrCol(i);

      ImageSlice *coluna = this->fits->getColumn(i, 5);

      fit->fit(coluna);

      delete coluna;
    
    }
    
    delete fit;

  }catch (IFUException e) {

    e.what();
    exit(1);

  }
  
}

void Mask::setCurrCol(int col) {
  currCol = col;
}

void Mask::setCurrPeak(int peak) {
  currPeak = peak;
}

int Mask::getCurrCol() {
  return currCol;
}

int Mask::getCurrPeak() {
  return currPeak;
}

const string &Mask::getFilename() {
  return filename;
}
